package com.interheart.framecore.util;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.Date;

/**
 * @Auther: SHIKAI
 * @Date: 2019/5/14 18:28
 */
@Data
@AllArgsConstructor
public class VerificationCodeUtil {
    private String code;
    private static final int effectiveTime = 60 * 5;
    private Date endTime;

    public VerificationCodeUtil(){

    }

    /**
     * 功能描述 发送验证码
     * @author SHIKAI
     * @date 2019/5/14
     * @param
     * @return
     */
    public  VerificationCodeUtil  sendCode(String phone){
        //随机验证码
        this.code = RandomUtil.randomNumbers(4);
        //发送短信

        return new  VerificationCodeUtil(code, DateUtil.date().offset(DateField.SECOND, effectiveTime));
    }
    
    /**
     * 功能描述 是否过期
     * @author SHIKAI
     * @date 2019/5/14
     * @param  
     * @return 
     */
    public boolean isOverdue(){
        return (System.currentTimeMillis() - this.endTime.getTime() ) / 1000 > 30;
    }

}
